package com.mes.schedule.algorithm;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.drools.KnowledgeBase;
import org.springframework.stereotype.Component;

import com.mes.common.rule.AbstractKnowledgeApplication;
import com.mes.schedule.algorithm.iface.ILinkLagCal;
import com.mes.schedule.domain.SOperationLink;
import com.mes.schedule.domain.SOperationTask;
import com.mes.schedule.domain.ScheduleScheme;

@Component("linkLagCal")
public class LinkLagCal   extends AbstractKnowledgeApplication implements ILinkLagCal{
	private static final Log log = LogFactory.getLog(LinkLagCal.class);
	
	public LinkLagCal(KnowledgeBase knowledgeBase) {
		super(knowledgeBase);
	}
	
	public LinkLagCal(){
		super();
	}
	
	public float getLinkLag(SOperationTask predSo, SOperationTask succSo) {
		// TODO Auto-generated method stub
//		float linkLag=0;

//		
//		linkLag=30;
//		
//		if(predSo.getMasterShop()!=  succSo.getMasterShop()!=succSo.getScheme().getDeptUid() )
//			if()
//				
//		
//		if(succSo.getMasterShop().substring(0,4)==predso.getMasterShop().substring(0,4))linkLag=120;	
//		if(succSo.getMasterShop().substring(0,4)<>predso.getMasterShop().substring(0,4))linkLag=120;	
//		if(succSo.getMasterShop()='CWXZ')linkLag=1440;
//		
//		if(predSo.)
//		SOperationTask
		
//		return linkLag;
		/*
		Float linkLag=0F;
		insertOrUpdate(predSo);
		insertOrUpdate(succSo);
		insertOrUpdate(linkLag);
		this.getKsession().fireAllRules();
		this.delete(predSo);
		this.delete(succSo);
		return linkLag;*/
		Float linkLag = 0F;
		if(predSo==null||succSo==null)
			return 0;
		if(predSo.getMasterShop()!= null){
		if (predSo.getMasterShop().equals(succSo.getMasterShop())) {
			linkLag = 120f;//todo 缺省为两小时，增加不同工序周转代价，应该参数化
		
		} 
//暂时屏蔽
//		else if (succSo.getMasterShop().substring(0, 4).equals(predSo
//				.getMasterShop().substring(0, 4))) {
//			linkLag = 60f;
//		} else if (succSo.getMasterShop().substring(0, 2).equals(predSo
//				.getMasterShop().substring(0, 2)) ) {
//			linkLag = 360f;
//		} else if (predSo.getMasterShop().equals("CWXZ")) {
////			linkLag = 1440f;
//		}
		}
		
		return linkLag;
		
		
	}

	protected void globalSetting() {
		// TODO Auto-generated method stub
		
	}

	public float getLinkLag(SOperationLink link) {
		// TODO Auto-generated method stub
		//if(link.getLinkLag()==null)
		
		return 0f;
	}

	public float getLinkLag(SOperationLink link, ScheduleScheme scheme) {
		// TODO Auto-generated method stub
		
		SOperationTask predSo=scheme.getAllOperationTaskMap().get(link.getPredecessorUid());
		SOperationTask succSo=scheme.getAllOperationTaskMap().get(link.getSuccessorUid());
		//if(link.getLinkLag()==null)
		return getLinkLag(predSo,succSo);
	}
	}



	




